﻿using System;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Windows;

namespace Game.Map
{
    [CollectionDataContract(Namespace="game",ItemName="Field")]
    public class GameFieldCollection : KeyedCollection<Point, IMapField>
    {
        public event EventHandler Changed;
        
        protected override void InsertItem(int index, IMapField item)
        {
            base.InsertItem(index, item);
            FireChanged();
        }
        
        protected override void RemoveItem(int index)
        {
            base.RemoveItem(index);
            FireChanged();
        }
        
        protected override void SetItem(int index, IMapField item)
        {
            base.SetItem(index, item);
            FireChanged();
        }
        
        protected override Point GetKeyForItem(IMapField item)
        {
            return item.Point;
        }
        
        void FireChanged()
        {
            if (Changed != null)
            {
                Changed(this, new EventArgs());
            }
        }
        
    }
}
